package edu.dao.impl;

import edu.dao.BasePersonDaoImpl;
import edu.domain.Person;
import edu.domain.Student;
import edu.uitls.FileUtils;

import java.io.File;
import java.util.ArrayList;

public class StudentDaoImpl extends BasePersonDaoImpl<Student> {
    static ArrayList<Student> list;
    static {
        list = (ArrayList<Student>) FileUtils.reSaveByObjectReaderList(new File("itheima-edu-manager(多态&泛型版)//student.txt"));
    }

    @Override
    public void addPerson(Student student) {
        list.add(student);
        FileUtils.reloadByObjectWriterList(list,new File("itheima-edu-manager(多态&泛型版)//student.txt"));
    }

    @Override
    public void delectPerson(int index) {
        list.remove(index);
        FileUtils.reloadByObjectWriterList(list,new File("itheima-edu-manager(多态&泛型版)//student.txt"));
    }

    @Override
    public void updatePerson(int index, Student student) {
        list.set(index, student);
        FileUtils.reloadByObjectWriterList(list,new File("itheima-edu-manager(多态&泛型版)//student.txt"));
    }

    @Override
    public ArrayList<Student> findAllPerson() {
        return list;
    }

    @Override
    public int getIndexById(String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
